<?php
/**
 * 管理员后台
 * User: henrick
 * Date: 2017/3/22
 * Time: 18:17
 */
namespace app\core;

use think\Controller;

class Outcontroller extends Controller
{
    protected $no_auth_url = [   //不用权限校验的url
        'index/index/index',
        'index/user/login',
        'index/index/type',
        'index/index/works',
        'index/index/article',
        'index/tshow/index',
        'index/hshow/index',
        'index/news/index'
    ];

    protected $login_no_auth_url = [  //登录不需要权限验证
        'admin/api/getqiniuuploadtoken',  //上传七牛云token
        'admin/api/delqiniures',          //七牛云资源删除接口
    ];

    protected function _initialize(){
        //echo url('index/index/tshow');
       // exit;
        $this->_routerAuth();
    }

    protected function _routerAuth(){  //权限校验
        $url = url('','',false);
        $url = ltrim(str_replace('/index.php/','',$url),'/');
        //echo $url;exit;
        //判断url是否是为不用权限校验
        if(in_array($url,$this->no_auth_url)){
            return true;
        }

        $admin_info = session('user_info');

        //判断是否登录
        $res = $this->_isLogin();
        if(!$res){
            $this->redirect(url('index/index/login'));
            exit;
        }

        if(in_array($url,$this->login_no_auth_url)){
            return true;
        }

        //判断是否有权限访问
        $auth_list = cache('cache_user_id_'.$admin_info['id']);
        if(in_array($url,$auth_list['user_auth'])){
            unset($auth_list,$admin_info);
            return true;
        }

        $this->redirect(url('index/index/page403'));
        exit;
    }

    protected function _isLogin(){   //是否登录
        $admin_info = session('user_info');
        if(!$admin_info){
            return false;
        }
        return true;
    }

    protected function show($path){
        $menu = $this->_getMenu();
        $this->assign('menu',$menu);
        echo $this->fetch('default/layout/header');
        echo $this->fetch('default/'.$path);
        echo $this->fetch('default/layout/footer');
    }

    protected function _getMenu(){  //获取菜单
        $menu = cache('menu');
        if($menu){
            return $menu;
        }
        $menu = model('menu');
        $menu = $menu->field('name,url')->select();

        cache('menu',$menu);
        return $menu;
    }
}